#include <stdlib.h>
#include <string.h>
#include <openssl/evp.h>

char iv[EVP_MAX_IV_LENGTH] = "my iv";

int
encrypt( char *ciphertext, char *plaintext, char *mykey )
{
	int in_len, out_len, term_len;	
	EVP_CIPHER_CTX ctx;
	
	in_len = strlen(plaintext);
	
    EVP_EncryptInit(&ctx, EVP_aes_128_cbc(), mykey, iv);
    
    EVP_EncryptUpdate(&ctx, ciphertext, &out_len, plaintext, in_len);
    
    EVP_EncryptFinal(&ctx, &ciphertext[out_len], &term_len);
    return out_len+term_len;
}

void
decrypt( char *unciphertext, char *ciphertext, int cipher_len, char *mykey )
{
	int in_len, out_len;	
	EVP_CIPHER_CTX ctx;
	
	
	EVP_DecryptInit(&ctx, EVP_aes_128_cbc(), mykey, iv);
	EVP_DecryptUpdate(&ctx, unciphertext, &in_len, ciphertext, cipher_len);
	printf("inlen =%d\n", in_len);
	EVP_DecryptFinal(&ctx, &unciphertext[in_len], &in_len);
	return;
}


int 
main(void) 
{
	char plaintext[1024] = "cataratas del Iguazu Misiones Argentinas,(20,2, 200)";
	char unciphertext[1024];
    char ciphertext[1024];
    char mykey[EVP_MAX_KEY_LENGTH] = "this's my key";
    
    int cipher_len;
    
    printf("No encrypt: [%s]\n", plaintext);
    cipher_len = encrypt( ciphertext, plaintext, mykey );
     
    printf("Encrypted : [%s]\n", ciphertext);
    decrypt( unciphertext, ciphertext, cipher_len, mykey );
    printf("Decrypted : [%s]\n", unciphertext);

    return 0;
}

